import logging
import re
from pydantic import Field
from nat.builder.builder import Builder
from nat.builder.function_info import FunctionInfo
from nat.cli.register_workflow import register_function
from nat.data_models.function import FunctionBaseConfig

logger = logging.getLogger(__name__)


class TimeBookFunctionConfig(FunctionBaseConfig, name="time_book"):
    """
    A function that extracts hour numbers from text and constructs a question:
    "{number} book name is what"
    """
    parameter: str = Field(default="default_value", description="Notional description for this parameter")


# @register_function(config_type=TimeBookFunctionConfig)
# async def time_book_function(
#     config: TimeBookFunctionConfig, builder: Builder
# ):
#     async def _response_fn(input_message: str) -> str:
#         # Use regex to find all numbers representing hours (0-23)
#         hour_matches = re.findall(r'\b(?:[01]?\d|2[0-3])\b', input_message)
        
#         if not hour_matches:
#             return "No valid hour found in the input."
        
#         # Take the first matched hour number
#         hour = hour_matches[0]
        
#         # Construct the question
#         question = f"{hour} book name is what"
        
#         return question

#     try:
#         yield FunctionInfo.from_fn(_response_fn,
#                                    description="Extracts hour numbers from text and asks for the corresponding book name.")
#     except GeneratorExit:
#         logger.warning("Function exited early!")
#     finally:
#         logger.info("Cleaning up time_book workflow.")
@register_function(config_type=TimeBookFunctionConfig)
async def time_book_function(config: TimeBookFunctionConfig, builder: Builder):
    async def _response_fn(input_message: str) -> str:
        # 仅匹配 yyyy-mm-dd HH:MM:SS 格式里的“小时”
        m = re.search(r'\b(?:[01]?\d|2[0-3])(?=:)', input_message)
        if not m:
            return "No valid hour found in the input."
        hour = m.group()
        return f"{hour} book name is what"

    try:
        yield FunctionInfo.from_fn(
            _response_fn,
            description="Extracts the hour (0-23) from yyyy-mm-dd HH:MM:SS and asks for the corresponding book name."
        )
    except GeneratorExit:
        logger.warning("Function exited early!")
    finally:
        logger.info("Cleaning up time_book workflow.")




# import logging

# from pydantic import Field

# from nat.builder.builder import Builder
# from nat.builder.function_info import FunctionInfo
# from nat.cli.register_workflow import register_function
# from nat.data_models.function import FunctionBaseConfig

# logger = logging.getLogger(__name__)


# class TimeBookFunctionConfig(FunctionBaseConfig, name="time_book"):
#     """
#     NAT function template. Please update the description.
#     """
#     # Add your custom configuration parameters here
#     parameter: str = Field(default="default_value", description="Notional description for this parameter")


# @register_function(config_type=TimeBookFunctionConfig)
# async def time_book_function(
#     config: TimeBookFunctionConfig, builder: Builder
# ):
#     # Implement your function logic here
#     async def _response_fn(input_message: str) -> str:
#         # Process the input_message and generate output
#         output_message = f"Hello from time_book workflow! You said: {input_message}"
#         return output_message

#     try:
#         yield FunctionInfo.create(single_fn=_response_fn)
#     except GeneratorExit:
#         logger.warning("Function exited early!")
#     finally:
#         logger.info("Cleaning up time_book workflow.")